package com.quasar.hpatient.view.personal.setup;
import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.text.InputFilter;
import android.widget.EditText;
import android.widget.Toast;

import com.hyphenate.EMCallBack;
import com.quasar.hpatient.MainAppliaction;
import com.quasar.hpatient.R;
import com.quasar.hpatient.base.BaseActivity;
import com.quasar.hpatient.model.event.CloseEvent;
import com.quasar.hpatient.model.httpmodel.DataStateResult;
import com.quasar.hpatient.onInterface.AnotherResultListener;
import com.quasar.hpatient.presenter.UpdatePwdPresenter;
import com.quasar.hpatient.utils.LogAndToastUtil;
import com.quasar.hpatient.utils.StringUtils;
import com.quasar.hpatient.utils.UrlUtils;
import com.quasar.hpatient.view.LoginActivity_;
import com.vise.log.ViseLog;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;

import chatui.DemoHelper;

/**
 * author : 许建宏
 * Date: 2017/10/8
 * View:修改密码
 */
@EActivity(R.layout.activity_update_pwd)
public class UpdatePwdActivity extends BaseActivity implements AnotherResultListener<DataStateResult> {


    private UpdatePwdPresenter updatePwdPresenter;

    @ViewById(R.id.main_toolbar)
    Toolbar main_toolbar;
    //旧密码
    @ViewById(R.id.et_update_ename)
    EditText oldPwd;
    //新密码
    @ViewById(R.id.et_update_password)
    EditText newPwd;
    //确认密码
    @ViewById(R.id.et_repeat_password)
    EditText confirmPwd;

    @AfterViews
    void init(){
        ViseLog.d("enter");
        initView();
        initData();
    }

    @Override
    protected void initView() {
        initToolbar();
        oldPwd.setFilters(new InputFilter[]{new InputFilter.LengthFilter(25)});
        newPwd.setFilters(new InputFilter[]{new InputFilter.LengthFilter(25)});
        confirmPwd.setFilters(new InputFilter[]{new InputFilter.LengthFilter(25)});
    }

    @Override
    protected void initData() {
        updatePwdPresenter=new UpdatePwdPresenter(this);
    }

    /**
     * 提交
     */
    @Click(R.id.bt_login)
    public void bt_login(){
        String strOldpwd=oldPwd.getText().toString().trim();
        String strNewPwd=newPwd.getText().toString().trim();
        String strConPwd=confirmPwd.getText().toString().trim();

        if(strOldpwd.length()==0){
            oldPwd.setError("旧密码不能为空");
            oldPwd.requestFocus();
            return;
        }

        if(strNewPwd.length()==0){
            newPwd.setError("新密码不能为空");
            newPwd.requestFocus();
            return;
        }

        if(!strConPwd.equals(strNewPwd)){
            confirmPwd.setError("两次密码输入不一致");
            confirmPwd.requestFocus();
            return;
        }

        String cinOldPwd= MainAppliaction.getInstance().getPWd();

        ViseLog.d("cinOldPwd"+cinOldPwd);
        if(!strOldpwd.equals(cinOldPwd)){
            oldPwd.setError("旧密码输入不正确");
            oldPwd.requestFocus();
            return;
        }

        if(strConPwd.length()<6){
            newPwd.setError("密码格式不正确");
            return;
        }

        if(!StringUtils.PwdContainsStr(strConPwd)){
            confirmPwd.setError("密码格式不正确");
            newPwd.setText("");
            confirmPwd.setText("");
            confirmPwd.requestFocus();
            return;
        }
        showDialog();
        updatePwdPresenter.UpdatePwdOperation(strOldpwd,strConPwd);
    }


    public void msg(String msg){
        LogAndToastUtil.toast(this,msg,"");
    }

    @Override
    public void onSuccess(DataStateResult data) {
        if(data.getMsgCode().equals("0x00000020")){
            msg("用户密码重置成功");
            String strConPwd=confirmPwd.getText().toString().trim();
            MainAppliaction.getInstance().setPwd(strConPwd);
            exitlog();
        }

    }

    //密码修改成功后退出环信
    public void exitlog(){
        EventBus.getDefault().post(new CloseEvent());
        DemoHelper.getInstance().logout(false, new EMCallBack() {
            @Override
            public void onSuccess() {
                runOnUiThread(new Runnable() {
                    public void run() {
                        dessDialog();
                        finish();
                        LoginActivity_.intent(UpdatePwdActivity.this).start();

                    }
                });
            }
            @Override
            public void onProgress(int progress, String status) {

            }

            @Override
            public void onError(int code, String message) {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        dessDialog();
                        Toast.makeText(UpdatePwdActivity.this, "unbind devicetokens failed", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

    @Override
    public void onError(String msg) {
        ViseLog.d("onError"+msg);
        if(msg!=null){
            msg(msg);
            dessDialog();
        }
    }

    @Override
    public void onRefreshUI() {
        ViseLog.d("onComplete");
    }
}

